<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class Carousel extends Model{
    protected $autoWriteTimestamp = false;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $field
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where = [], $order = '', $limit = 10, $field = '', $out = false){
        $list = $this
            ->field($field,$out)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $page = $list->currentPage();
        $total = $list->total();
        foreach ($list as $k => $v){
            $list[$k]['sort_asc'] = $k + 1 + ($page-1) * $limit;
            $list[$k]['sort_desc'] = $total - ($page-1) * $limit - $k;
        }
        return $list;
    }

    /**
     * @param $id 排序id
     * @param $type 排序类型 1升序 2降序
     * @param array $search_data
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function setAdSort($id,$type,$search_data=[]){
        $old_sort = $this->field('order_id,((1-`status`)*100000+`order_id`) as orders')->find($id);
        $old_sort = !empty($old_sort) ? array_values($old_sort->toArray()) : array(1);
        $old_sort = $old_sort[0];
        $where = array();
        if(!empty($search_data)){
            if(!empty($search_data['type'])){
                $where[] = ['type','eq',$search_data['type']];
            }
        }
        if($type==1){
            $last_info = $this
                ->where($where)
                ->having("order_id < ".$old_sort)
                ->order('orders desc')
                ->field('id,order_id,((1-`status`)*100000+`order_id`) as orders')
                ->find();
        }else{
            $last_info = $this
                ->where($where)
                ->having("orders > ".$old_sort)
                ->order('orders asc')
                ->field('id,order_id,((1-`status`)*100000+`order_id`) as orders')
                ->find();
        }
        if(empty($last_info)){
            return false;
        }
        $res1 = $this->isUpdate(true)->save(['id'=>$id,'order_id'=>$last_info['order_id']]);
        $res2 = $this->isUpdate(true)->save(['id'=>$last_info['id'],'order_id'=>$old_sort]);
        if($res1 && $res2){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if($value){
            return (new Pic())->getUrlsById($value);
        }else{
            return '';
        }
    }
}